package cc.co.dreamind;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class dbHelper extends SQLiteOpenHelper {

	public static final String TB1_NAME = "account1";
	public static final String TB2_NAME = "account2";
	public static final String TB3_NAME = "account3";
	public static final String TB4_NAME = "reasons";
	public static final String ID = "_id";
	public static final String REASON = "reason";
	public static final String AMOUNT = "amount";
	public static final String TIME = "time";

	// table_id
	static final int TB1_ID = 1;
	static final int TB2_ID = 2;
	static final int TB3_ID = 3;
	static final int TB_REASON_ID = 4;

	public dbHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}

	public void Close() {
		this.getWritableDatabase().close();
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub

		// database
		/*
		 * current required tables:
		 * table1(account1):id(int),time(string),reason(string),amount(float);
		 * table2(account2):as above; 
		 * table3(account3):as above;
		 * table4(reasons):id(int),content(string),amount(float);
		 */
		// add the check whether the talbe is EXISTS firtly
		String sql = "CREATE  TABLE IF NOT EXISTS account1 (_id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , time TEXT, reason TEXT,amount REAL)";
		db.execSQL(sql);
		sql = "CREATE  TABLE IF NOT EXISTS account2 (_id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , time TEXT, reason TEXT,amount REAL)";
		db.execSQL(sql);
		sql = "CREATE  TABLE IF NOT EXISTS account3 (_id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , time TEXT, reason TEXT,amount REAL)";
		db.execSQL(sql);
		sql = "CREATE  TABLE IF NOT EXISTS reasons (_id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , reason TEXT, amount REAL)";
		db.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

	public void addterm(String reason, String amount, String time, int table_id) {

		ContentValues values = new ContentValues();
		values.put(dbHelper.REASON, reason);
		values.put(dbHelper.TIME, time);
		values.put(dbHelper.AMOUNT, amount);
		switch (table_id) {
		case TB1_ID:
			this.getWritableDatabase().insert(dbHelper.TB1_NAME, dbHelper.ID,
					values);
			break;
		case TB2_ID:
			this.getWritableDatabase().insert(dbHelper.TB2_NAME, dbHelper.ID,
					values);
			break;
		case TB3_ID:
			this.getWritableDatabase().insert(dbHelper.TB3_NAME, dbHelper.ID,
					values);
			break;
		case TB_REASON_ID:
			this.getWritableDatabase().insert(dbHelper.TB3_NAME, dbHelper.ID,
					values);
			break;
		}

	}

	public void addreason(int reason_id,String reason,String amount) {
		ContentValues values = new ContentValues();
		values.put(dbHelper.ID, reason_id);
		values.put(dbHelper.REASON, reason);
		values.put(dbHelper.AMOUNT, amount);
		this.getWritableDatabase().replace("reasons", dbHelper.ID, values);
	}

	public Cursor query(String TBL_NAME) {
		SQLiteDatabase db = getWritableDatabase();
		Cursor c = db.query(TBL_NAME, null, null, null, null, null, null);
		return c;
	}
	
/**
 * Database delete 
 * db.delete("Table Name", "ColoumNmae='String'", null);
 * */
}
